﻿(function($) {
    $.extend({
        metadata: {
            defaults: {
                type: 'class',
                name: 'metadata',
                cre: /({.*})/,
                single: 'metadata'
            },
            setType: function(type, name) {
                this.defaults.type = type;
                this.defaults.name = name;
            },
            get: function(elem, opts) {
                var settings = $.extend({}, this.defaults, opts);
                // check for empty string in single property
                if (!settings.single.length) settings.single = 'metadata';

                var data = $.data(elem, settings.single);
                // returned cached data if it already exists
                if (data) return data;

                data = "{}";

                if (settings.type == "class") {
                    var m = settings.cre.exec(elem.className);
                    if (m)
                        data = m[1];
                } else if (settings.type == "elem") {
                    if (!elem.getElementsByTagName)
                        return undefined;
                    var e = elem.getElementsByTagName(settings.name);
                    if (e.length)
                        data = $.trim(e[0].innerHTML);
                } else if (elem.getAttribute != undefined) {
                    var attr = elem.getAttribute(settings.name);
                    if (attr)
                        data = attr;
                }

                if (data.indexOf('{') < 0)
                    data = "{" + data + "}";

                data = eval("(" + data + ")");

                $.data(elem, settings.single, data);
                return data;
            }
        }
    });

    /**
    * Returns the metadata object for the first member of the jQuery object.
    *
    * @name metadata
    * @descr Returns element's metadata object
    * @param Object opts An object contianing settings to override the defaults
    * @type jQuery
    * @cat Plugins/Metadata
    */
    $.fn.metadata = function(opts) {
        return $.metadata.get(this[0], opts);
    };

})(jQuery);

/*
* Translated default messages for the jQuery validation plugin.
* Locale: CN
*/
jQuery.extend(jQuery.validator.messages, {
    required: "必选字段",
    remote: "请修正该字段",
    email: "请输入正确格式的电子邮件",
    url: "请输入合法的网址",
    date: "请输入合法的日期",
    dateISO: "请输入合法的日期 (ISO).",
    number: "请输入合法的数字",
    digits: "只能输入整数",
    creditcard: "请输入合法的信用卡号",
    equalTo: "请再次输入相同的值",
    accept: "请输入拥有合法后缀名的字符串",
    maxlength: jQuery.validator.format("请输入一个长度最多是 {0} 的字符串"),
    minlength: jQuery.validator.format("请输入一个长度最少是 {0} 的字符串"),
    rangelength: jQuery.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
    range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
    max: jQuery.validator.format("请输入一个最大为 {0} 的值"),
    min: jQuery.validator.format("请输入一个最小为 {0} 的值")
});

$(document).ready(function() {
    $("#showcurrenthtml").click(function() {
        alert($('#mtable').html());
    });
});

$(document).ready(function() {

    /**//* 设置默认属性 */
    $.validator.setDefaults({
        submitHandler: function(form) { form.submit(); }, //默认的提交方式
        errorElement: "div", //提示使用的标签
        errorClass: "check_tips", //提示使用的类
        errorPlacement: function(error, element) {
            if (element.is(":radio")) { element.parent().append(error); }
            else if (element.is(":checkbox")) { element.parent().append(error); }
            //else{error.insertAfter(element);}
            else { element.parent().append(error); }
        },
        /**//* 设置验证触发事件 */
        onsubmit: true,
        focusInvalid: false,
        onfocusin: false,
        onfocusout: false,
        onkeyup: false,
        onclick: false,
        highlight: false,
        unhighlight: false,

        //自定义错误显示
        showErrors: function(errorMap, errorList) {
            var firstElement = errorList[0];
            if (firstElement) {
                var txtcontrol = firstElement.element;
                var txtobject = $("#" + txtcontrol.id);
                if (txtobject && txtobject.css("display") != "none") {
                    txtobject.focus();
                    var txtpos = txtobject.offset().top;
                    if (txtpos > 120) { txtpos = txtpos - 120; }
                    window.scroll(0, txtpos);
                }
            }
            var errorNum = this.numberOfInvalids();
            if (errorNum > 0) { _M.Check("请更正以下" + this.numberOfInvalids() + "个错误！"); }
            this.defaultShowErrors();
        },

        //success: function(label) { label.text("").addClass("success"); } //提示标签使用的类
        success: function(label) { label.remove(); } //提示标签使用的类

    });

    //字符验证       
    jQuery.validator.addMethod("stringCheck", function(value, element) {
        return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
    }, "只能包括中文字、英文字母、数字和下划线");

    //中文字两个字节       
    jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {
        var length = value.length;
        for (var i = 0; i < value.length; i++) {
            if (value.charCodeAt(i) > 127) {
                length++;
            }
        }
        return this.optional(element) || (length >= param[0] && length <= param[1]);
    }, "请确保输入的值在{0}-{1}个字节之间(一个中文字算2个字节)");

    //至少输入几个字       
    jQuery.validator.addMethod("minlengthCn", function(value, element, param) {
        var length = value.length;
        for (var i = 0; i < value.length; i++) {
            if (value.charCodeAt(i) > 127) {
                length++;
            }
        }
        return this.optional(element) || length >= param;
    }, "请至少输入{0}个字节(一个中文字算2个字节)");

    //至多输入几个字       
    jQuery.validator.addMethod("maxlengthCn", function(value, element, param) {
        var length = value.length;
        for (var i = 0; i < value.length; i++) {
            if (value.charCodeAt(i) > 127) {
                length++;
            }
        }
        return this.optional(element) || length <= param;
    }, "请至多输入{0}个字节(一个中文字算2个字节)");

    //身份证号码验证       
    jQuery.validator.addMethod("isIdCardNo", function(value, element) {
        return this.optional(element) || isIdCardNo(value);
    }, "请正确输入您的身份证号码");

    //手机号码验证       
    jQuery.validator.addMethod("isMobile", function(value, element) {
        var length = value.length;
        var mobile = /^0?1(3|4|8|5)[0-9]{9}$/;
        return this.optional(element) || (length == 11 && mobile.test(value));
    }, "请正确填写您的手机号码");

    //电话号码验证       
    jQuery.validator.addMethod("isTel", function(value, element) {
        var tel = /^\d{3,4}-\d{7,8}$|^\d{3,4}-\d{7,8}-\d+$/;    //电话号码格式010-12345678-12312312  
        return this.optional(element) || (tel.test(value));
    }, "请正确填写您的电话号码");

    //联系电话(手机/电话皆可)验证   
    jQuery.validator.addMethod("isPhone", function(value, element) {
        var length = value.length;
        var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
        var tel = /^\d{3,4}-?\d{7,9}$/;
        return this.optional(element) || (tel.test(value) || mobile.test(value));
    }, "请正确填写您的联系电话");

    //邮政编码验证       
    jQuery.validator.addMethod("isZipCode", function(value, element) {
        var tel = /^[0-9]{6}$/;
        return this.optional(element) || (tel.test(value));
    }, "请正确填写您的邮政编码");

    //下拉框的选择项数值必须大于0的自然数       
    jQuery.validator.addMethod("MustSelZRS", function(value, element) {
        return value > 0;
    }, "下拉框的选择项数值必须大于0的自然数");

    //验证2位小数       
    jQuery.validator.addMethod("isLianWeiXiaoShu", function(value, element) {
        var tel = /^\d{1,5}(\.(\d{1,2}))?$/;
        return this.optional(element) || (tel.test(value));
    }, "请填写小数点后两位小数");

});


function isIdCardNo(num) {
    if (isNaN(num)) { return false; }
    var len = num.length, re;
    if (len == 15)
        re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
    else if (len == 18)
        re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/);
    else { return false; }
    var a = num.match(re);
    if (a != null) {
        if (len == 15) {
            var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
            var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
        }
        else {
            var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
            var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
        }
        if (!B) { return false; }
    }
    return true;
}
